﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'proc_cm_SaveFileshare')
BEGIN
    PRINT 'Dropping Procedure proc_cm_SaveFileshare'
    DROP  Procedure  proc_cm_SaveFileshare
END
GO

PRINT 'Creating Procedure proc_cm_SaveFileshare'
GO

CREATE PROCEDURE [dbo].[proc_cm_SaveFileshare]
	@pFileshareId uniqueidentifier
    , @pHtmlPageId uniqueidentifier
    , @pSubjectTxt nvarchar(512)
    , @pDescriptionTxt nvarchar(max) = null
    , @pFilename nvarchar(100)
	, @pContentType nvarchar(50)
	, @pSectionCd nvarchar(50)
AS
BEGIN
	
	SET NOCOUNT ON

	UPDATE
		[dbo].[tbl_fileshare]
	SET 
		[subject_txt] = @pSubjectTxt
		,[filename_txt] = @pFilename
		,[description_txt] = @pDescriptionTxt
		,[content_type] = @pContentType
		,[update_dttm] = GETDATE()
	WHERE
		[fileshare_id] = @pFileshareId
		AND [section_cd] = @pSectionCd

	if @@ROWCOUNT = 0
	begin
		INSERT INTO [dbo].[tbl_fileshare]
		(
			[fileshare_id]
			,[html_page_id]
			,[subject_txt]
			,[filename_txt]
			,[description_txt]
			,[content_type] 
			,[create_dttm]
			,[update_dttm]
			,[section_cd]
		)
		SELECT
			@pFileshareId
			, @pHtmlPageId
			, @pSubjectTxt
			, @pFilename
			, @pDescriptionTxt
			, @pContentType
			, GETDATE()
			, GETDATE()
			, @pSectionCd
	end
END
GO

GRANT EXEC ON dbo.proc_cm_SaveFileshare TO PUBLIC
GO
